package ldh.im.fxgui.controller.sub;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.Pane;
import javafx.stage.Popup;
import ldh.im.dto.enums.MessageType;
import ldh.im.fxbase.util.DialogUtil;
import ldh.im.fxbase.util.StringUtil;
import ldh.im.fxgui.pojo.RecordSettingData;

import java.net.URL;
import java.util.ResourceBundle;
import java.util.function.Consumer;

public class RecordScreenSettingController implements Initializable {

    @FXML private TextField delayTimeTextField;
    @FXML private TextField recordTotalTimeTextField;
    @FXML private TextField recordTotalTime2TextField;
    @FXML private TextField frameRateTextField;
    @FXML private RadioButton gifRadio;
    @FXML private RadioButton mp4Radio;
    @FXML private Pane gifPane;
    @FXML private Pane mp4Pane;

    private ToggleGroup selectTypeGroup = new ToggleGroup();
    private Popup popup;
    private Consumer<RecordSettingData> dataCallBack;

    public void setPopup(Popup popup) {
        this.popup = popup;
    }

    public void setDataCallBack(Consumer<RecordSettingData> dataCallBack) {
        this.dataCallBack = dataCallBack;
    }

    public void saveEvt(ActionEvent actionEvent) {
        RecordSettingData data = new RecordSettingData();
        data.setMessageType(gifRadio.isSelected() ? MessageType.Image : MessageType.Video);
        if (gifRadio.isSelected()) {
            String value = delayTimeTextField.getText();
            if (!StringUtil.isNumber(value)) {
                DialogUtil.stageAlert("错误", "帧长必须整数");
                return;
            }
            int delayTime = Integer.parseInt(value);
            if (delayTime < 100) {
                DialogUtil.stageAlert("错误", "帧长必须大于100");
                return;
            }
            data.setDelayTime(delayTime);

            value = recordTotalTimeTextField.getText();
            if (!StringUtil.isNumber(value)) {
                DialogUtil.stageAlert("错误", "录制时间必须整数");
                return;
            }
            int recordTotalTime = Integer.parseInt(value);
            if (recordTotalTime > 50) {
                DialogUtil.alert("错误", "录制时间不能超过50秒");
                return;
            }
            data.setRecordTotalTime(recordTotalTime);
        } else if (mp4Radio.isSelected()) {
            String value = frameRateTextField.getText();
            if (!StringUtil.isNumber(value)) {
                DialogUtil.stageAlert("错误", "帧率必须整数");
                return;
            }
            int frameRate = Integer.parseInt(value);
            if (!(frameRate > 10 && frameRate < 60)) {
                DialogUtil.stageAlert("错误", "帧率必须在10到60之间");
                return;
            }
            data.setFrameRate(frameRate);

            value = recordTotalTime2TextField.getText();
            if (!StringUtil.isNumber(value)) {
                DialogUtil.stageAlert("错误", "录制时间必须整数");
                return;
            }
            int recordTotalTime = Integer.parseInt(value);
            if (recordTotalTime < 5) {
                DialogUtil.alert("错误", "录制时间不能小于5秒");
                return;
            }
            data.setRecordTotalTime(recordTotalTime);
        } else {
            DialogUtil.stageAlert("错误", "请选择录制类型");
        }
        dataCallBack.accept(data);
        popup.hide();
    }

    public void closeEvt(ActionEvent actionEvent) {
        popup.hide();
    }

    public void resetEvt(ActionEvent actionEvent) {
        selectTypeGroup.selectToggle(null);
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        gifRadio.setToggleGroup(selectTypeGroup);
        mp4Radio.setToggleGroup(selectTypeGroup);

        selectTypeGroup.selectedToggleProperty().addListener((l)->{
            if (selectTypeGroup.getSelectedToggle() == gifRadio) {
                gifPane.setDisable(false);
                mp4Pane.setDisable(true);
            } else if(selectTypeGroup.getSelectedToggle() == mp4Radio) {
                gifPane.setDisable(true);
                mp4Pane.setDisable(false);
            } else {
                gifPane.setDisable(true);
                mp4Pane.setDisable(true);
            }
        });

        frameRateTextField.setText("30");
        delayTimeTextField.setText("200");
    }
}
